<?php
include_once ('class/autoload.php'); // pour inclure nos classes
include_once ('class/config.inc.php');
$site = new page_securisee ( 'Modification Pb', "Modification de la problèmatique", "" );

if (! isset ( $_POST ['boolModifier'] )) {
	$corps = '<form action="Modifierproblematique.php" method="post">';
	$requete = 'SELECT * FROM problematique';
	$result = $connexion->query ( $requete );
	$result->setFetchMode ( PDO::FETCH_OBJ );
	$corps .= '<table class="table table-bordered">
		<caption><h2>CHANGER LA VISIBILITE DES ITEMS</h2></caption>
		<thead> <!-- En-t�te du tableau -->
       <tr>
           <th>Probl�mes</th>
           <th>Visible</th>
			<th>Changer la visibilit�</th>
       </tr>
   </thead>
		';
	
	$i = 1;
	while ( $OneRow = $result->fetch () ) {
		$corps .= '<tr><td>' . $OneRow->nature . '</td>';
		
		if ($OneRow->actif == 1) {
			$corps .= '<td><p id="demo' . $i . '">Oui</p></td><td><input type="button" onclick="myFunction(' . $i . ')" value="Changer"</button></td><input type="hidden" id="actifBool' . $i . '" name="boolModifier[' . $i . ']" value="1"><input type="hidden" id="actifBool' . $i . '" name="idModifier[' . $i . ']" value="' . $OneRow->id . '"></tr>';
		}
		
		if ($OneRow->actif == 0) {
			$corps .= '<td><p id="demo' . $i . '">Non</p></td><td><input type="button" onclick="myFunction(' . $i . ')" value="Changer"></button></td><input type="hidden" id="actifBool' . $i . '" name="boolModifier[' . $i . ']" value="0"><input type="hidden" id="actifBool' . $i . '" name="idModifier[' . $i . ']" value="' . $OneRow->id . '"></tr>';
		}
		$i ++;
	}
	?>
<script type="text/javascript">
function myFunction(index){
	test=$('#demo'+index).text();
	value=$('#demo'+index).text();
	if(value=="Oui"){
			$('#demo'+index).text("Non");
			$('#actifBool'+index).val(0);
	}
	if(value=="Non"){
		$('#demo'+index).text("Oui");
		$('#actifBool'+index).val(1);
	}	
// 	var stre = document.getElementById("demo").innerHTML; 
// 	if(stre = "Oui"){
// 		 var res = stre.replace("Oui","Non");
// 		    document.getElementById("demo").innerHTML = res;
// 	}
// 	else{
// 		var rese = stre.replace("Non","Oui");
// 	    document.getElementById("demo").innerHTML = rese;
// 	}
	   
}
</script>
<?php
	/*
	 * $i = 1;
	 * foreach ( $problematique as $element ) {
	 * '<tr><td>' .$element[$i]->nature . '</td></tr>';
	 * }
	 */
	$corps .= '<tfooter> <!-- Pied du tableau -->
       <tr>
			<td colspan="3"><input type="submit" class="btn btn-large btn-primary" value="Modifier les priorit�s"></td>
       </tr>
   </tfooter>
   </table>
   </form>';
	
	$site->corps = $corps;
} else {
	$i = 1;
	$count = count ( $_POST ['boolModifier'] ) + 1;
	while ( $i < $count ) {
		$requete = 'UPDATE problematique set actif = "' . $_POST ['boolModifier'] [$i] . '" WHERE id = "' . $_POST ['idModifier'] [$i] . '";';
		$result = $connexion->query ( $requete );
		$i ++;
	}
	$corps = '<br><a href="AjoutTicket.php"> <input type="button" class="btn btn-large btn-primary" value="Ajouter un Ticket">  </a><a href="AccueilConnecter.php"> <input type="button"  class="btn btn-large btn-primary" value="Accueil">  </a>';
	$site->corps = $corps;
}
$site->affiche ();